package com.quan.server.service;

import com.mark.util.CommonUtil;
import com.quan.server.beans.Income;
import com.quan.server.helper.MyAppHelper;
import com.quan.server.jpa.IncomeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

@Component
@Path("/incomes")
public class IncomeService {
    private static Logger logger = LoggerFactory.getLogger(IncomeService.class);
    @Autowired
    private IncomeRepository incomeRepository;

    @Autowired
    private MyAppHelper myAppHelper;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(Income income) {
        try {
            income.id = CommonUtil.createRandomId();
            income = incomeRepository.save(income);
            return Response.status(Response.Status.CREATED).entity(income).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response delete(@PathParam("id") String id) {
        try {
            incomeRepository.delete(id);
            return Response.status(Response.Status.NO_CONTENT).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response put(Income income) {
        try {
            if (CommonUtil.isEmpty(income.id)) {
                income.id = CommonUtil.createRandomId();
            }
            income = incomeRepository.save(income);
            return Response.ok(income).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@PathParam("id") String id) {
        try {
            Income income = incomeRepository.findOne(id);
            if (income == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(income).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAll() {
        try {
            List<Income> incomeList = incomeRepository.findAll();
            return Response.ok(incomeList).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("branch")
    public Response post(List<Income> incomeList) {
        for (Income Income : incomeList) {
            post(Income);
        }
        return Response.ok().build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getIncomeWithRequireWithdraw")
    public Response getIncomeWithRequireWithdraw(@QueryParam("id") String id, @QueryParam("money") double money) {
        try {
            Income income = myAppHelper.getIncomeRequireWithdraw(id, money);
            if (income == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(income).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getEnableWithdrawMoney")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getEnableWithdrawMoney(@QueryParam("id") String id) {
        try {
            Income income = incomeRepository.findOne(id);
            if (income == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                double enableWithdrawMoney = Income.getEnableWithdrawMoney(income);
                return Response.ok(enableWithdrawMoney).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/getIncomeWithFinishWithdrawMoney")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getIncomeWithFinishWithdrawMoney(@QueryParam("id") String id) {
        try {
            Income income = myAppHelper.getIncomeWithFinishWithdrawMoney(id);
            if (income == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(income).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }
}
